﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web.Services;
using System.Data.Entity;
using Newtonsoft.Json;
public partial class UserManagement_RoleManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Profile.Name = "waqas";
        //Profile.Department = "CSD";        
        //Profile.Save();

        using (POSEntities db = new POSEntities())
        {
            var roles = (from r in db.Roles
                         where r.BranchId == 1 && r.Active == true
                         select r).ToList();

                rptRoles.DataSource = roles;
            rptRoles.DataBind();            
        }
    }
    [WebMethod]
    public static string GetModuleRights()
    {
        string result;
        List<ModuleRights> listModuleRights = new List<ModuleRights>();
        using (POSEntities db = new POSEntities())
        {
            var moduleMain = (from m in db.ModuleMains
                where m.BranchId == 1 && m.Active
                select m).ToList();

            var moduleSub =
            (from ms in db.ModuleSubs
                join mm in db.ModuleMains on ms.MMId equals mm.MMId
                where ms.Active && mm.Active && mm.BranchId == 1
                select ms).ToList();

            var moduleRights =
            (from mr in db.ModuleRights
                join ms in db.ModuleSubs on mr.MSId equals ms.MSId
                join mm in db.ModuleMains on ms.MMId equals mm.MMId
                where mr.Active && mm.Active && ms.Active && mm.BranchId == 1
                select mr).ToList();

            ModuleRights objModuleRights = new ModuleRights
            {
                ListModuleMain = moduleMain.ToList(),
                ListModuleSub = moduleSub,
                ListModuleRights = moduleRights
            };
            listModuleRights.Add(objModuleRights);
            
            result = JsonConvert.SerializeObject(listModuleRights, Formatting.Indented,
                new JsonSerializerSettings
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.All
                });
        }
        return result;
    }

    [WebMethod]
    public static List<RoleRight> GetRoleRights(string RoleId)
    {   
        using (POSEntities db = new POSEntities())
        {
            int roleId = int.Parse(RoleId);
            var roleRights = (from r in db.RoleRights where r.RoleId== roleId select r).ToList();
            return roleRights;
        }
        
    }
    [WebMethod]
    public static List<Role> SaveRole(string role)
    {
        try
        {
            using (POSEntities db = new POSEntities())
            {
                Role objRole = new Role();
                objRole.RoleName = role;                ;
                objRole.BranchId = 1;
                objRole.Active = true;
                db.Roles.Add(objRole);
                db.SaveChanges();

                var roles = (from r in db.Roles
                             where r.BranchId == 1 && r.Active == true
                             select r).ToList();
                return roles;
            }
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }
        
    }
    [WebMethod]
    public static void SaveRoleRights(List<RoleRight> ListRoleRight,string RoleId)
    {
        try
        {
            using (POSEntities db = new POSEntities())
            {
                db.DeleteRoleRights(int.Parse(RoleId));
                foreach (var item in ListRoleRight)
                {
                    var roleRight = new RoleRight
                    {
                        MRId = item.MRId,
                        RoleId = item.RoleId
                    };
                    db.RoleRights.Add(roleRight);
                    db.SaveChanges();
                }

            }
        }
        catch
        {

        }
    }
}

public class ModuleRights
{
    public List<ModuleMain> ListModuleMain { get; set; }
    public List<ModuleSub> ListModuleSub { get; set; }
    public List<ModuleRight> ListModuleRights { get; set; }
}
